package com.chuqiyun.authorization.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuqiyun.authorization.annotation.AdminApiCheck;
import com.chuqiyun.authorization.entity.Bill;
import com.chuqiyun.authorization.entity.User;
import com.chuqiyun.authorization.service.BillService;
import com.chuqiyun.authorization.service.UserService;
import com.chuqiyun.authorization.utils.ResponseResult;
import com.chuqiyun.authorization.utils.exception.UnauthorizedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author mryunqi
 * @date 2023/4/22
 */
@RestController
public class SysBillController {
    @Value("${config.admin_path}")
    private String ADMIN_PATH;

    @Resource
    private BillService billService;

    @Resource
    private UserService userService;

    /**
    * @Author: mryunqi
    * @Description: 获取指定页面的账单数据
    * @DateTime: 2023/4/22 20:05
    * @Params: int page 页码
    * @Params: int size 每页数据量
     * @Return ResponseResult<Page<Bill>>
    */
    @AdminApiCheck
    @GetMapping("/{adminPath}/getBillList")
    public ResponseResult<Page<Bill>> getBillList(@PathVariable("adminPath") String adminPath,
                                                  @RequestParam(value = "page",defaultValue = "1") int page,
                                                  @RequestParam(value = "size",defaultValue = "20") int size)
            throws UnauthorizedException {
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }

        return ResponseResult.ok(billService.getBillList(page, size));
    }

    /**
    * @Author: mryunqi
    * @Description: 获取指定页是支付宝支付的账单
    * @DateTime: 2023/4/22 20:12
    */
    @AdminApiCheck
    @GetMapping("/{adminPath}/getBillListByAliPay")
    public ResponseResult<Page<Bill>> getBillListByAliPay(@PathVariable("adminPath") String adminPath,
                                                          @RequestParam(value = "page",defaultValue = "1") int page,
                                                          @RequestParam(value = "size",defaultValue = "20") int size)
            throws UnauthorizedException {
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }

        QueryWrapper<Bill> bizQueryWrapper = new QueryWrapper<>();
        bizQueryWrapper.eq("payment_type","AliPay");
        return ResponseResult.ok(billService.getBillList(page, size,bizQueryWrapper));
    }

    /**
    * @Author: mryunqi
    * @Description: 带参数查询某用户的账单
    * @DateTime: 2023/4/23 23:02
    */
    @AdminApiCheck
    @GetMapping("/{adminPath}/getBillListByUser")
    public ResponseResult<Page<Bill>> getBillListByUserId(@PathVariable("adminPath") String adminPath,
                                                          @RequestParam(value = "userParam") String userParam,
                                                          @RequestParam(value = "paramValue") String paramValue,
                                                          @RequestParam(value = "page",defaultValue = "1") int page,
                                                          @RequestParam(value = "size",defaultValue = "20") int size)
            throws UnauthorizedException {
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        QueryWrapper<Bill> bizQueryWrapper = new QueryWrapper<>();
        if ("id".equals(userParam)){
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id",paramValue);
            User user = userService.getOne(userQueryWrapper);
            bizQueryWrapper.eq("user_uuid",user.getUuid());
        }
        if ("phone".equals(userParam)){
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("phone",paramValue);
            User user = userService.getOne(userQueryWrapper);
            bizQueryWrapper.eq("user_uuid",user.getUuid());
        }
        if ("email".equals(userParam)){
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("email",paramValue);
            User user = userService.getOne(userQueryWrapper);
            bizQueryWrapper.eq("user_uuid",user.getUuid());
        }
        return ResponseResult.ok(billService.getBillList(page,size,bizQueryWrapper));
    }

    /**
    * @Author: mryunqi
    * @Description: 获取指定参数下检索到的账单列表
    * @DateTime: 2023/4/23 23:19
    */
    @AdminApiCheck
    @GetMapping("/{adminPath}/getBillListByParam")
    public ResponseResult<Page<Bill>> getBillListByParam(@PathVariable("adminPath") String adminPath,
                                                         @RequestParam(value = "param") String param,
                                                         @RequestParam(value = "paramValue") String paramValue,
                                                         @RequestParam(value = "page",defaultValue = "1") int page,
                                                         @RequestParam(value = "size",defaultValue = "20") int size)
            throws UnauthorizedException {
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        QueryWrapper<Bill> bizQueryWrapper = new QueryWrapper<>();
        if ("state".equals(param)){
            bizQueryWrapper.eq("state",paramValue);
        }
        if ("orderId".equals(param)){
            bizQueryWrapper.eq("order_id",paramValue);
        }
        if ("outOrderId".equals(param)){
            bizQueryWrapper.eq("out_order_id",paramValue);
        }
        if ("paymentType".equals(param)){
            bizQueryWrapper.eq("payment_type",paramValue);
        }
        return ResponseResult.ok(billService.getBillList(page,size,bizQueryWrapper));
    }

    /**
    * @Author: mryunqi
    * @Description: 修改账单
    * @DateTime: 2023/4/23 23:36
    */
    @AdminApiCheck
    @PostMapping("/{adminPath}/updateBill")
    public ResponseResult<String> updateBill(@PathVariable("adminPath") String adminPath,
                                             @RequestBody Bill bill)
            throws UnauthorizedException{
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        Bill billInfo = billService.getById(bill.getId());
        if (Objects.isNull(billInfo)){
            return ResponseResult.fail("该账单不存在");
        }
        if (billService.updateById(bill)){
            return ResponseResult.ok("修改成功");
        }
        return ResponseResult.fail("修改失败");
    }

    /**
    * @Author: mryunqi
    * @Description: 删除账单
    * @DateTime: 2023/4/24 22:00
    */
    @AdminApiCheck
    @PostMapping("/{adminPath}/deleteBill")
    public ResponseResult<String> deleteBill(@PathVariable("adminPath") String adminPath,
                                             @RequestParam(value = "id") int id)
            throws UnauthorizedException{
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        Bill billInfo = billService.getById(id);
        if (Objects.isNull(billInfo)){
            return ResponseResult.fail("该账单不存在");
        }
        if (billService.removeById(id)){
            return ResponseResult.ok("删除成功");
        }
        return ResponseResult.fail("删除失败");
    }
}
